import { Navigate, Outlet, useLocation } from '@umijs/max';
import { useModel } from '@umijs/max';
import { stringify } from 'querystring';

export default () => {
  const { initialState } = useModel('@@initialState');
  const location = useLocation();

  // 如果没有登录，重定向到登录页
  if (!initialState?.currentUser) {
    return <Navigate to={`/user/login?${stringify({ redirect: location.pathname })}`} />;
  }

  return <Outlet />;
};
